using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace Hydrogen.ULib.UINavigation
{
    [RequireComponent(typeof(UINavNode))]
    public class UISlider : UISelectableBase,
        IUINavNodeMovedHandle,
        IBeginDragHandler,
        IEndDragHandler
    {
        [Header(nameof(UISlider))]
        [SerializeField]
        private Slider _slider;

        private void Start()
        {
            var node = GetComponent<UINavSelectable>();
            if (this._slider)
            {
                switch (_slider.direction)
                {
                    case Slider.Direction.LeftToRight:
                    case Slider.Direction.RightToLeft:
                        node.MoveableArrow &= ~(Arrow.Left | Arrow.Right);
                        break;
                    case Slider.Direction.BottomToTop:
                    case Slider.Direction.TopToBottom:
                        node.MoveableArrow &= ~(Arrow.Up | Arrow.Down);
                        break;
                }
            }
        }

        private const float _delta = 0.05f;
        private float _targetVal;
        void IUINavNodeMovedHandle.OnMoved(Arrow arrow, IUINavSelectable next)
        {
            if (!_slider) return;

            _targetVal = this._slider.value;
            switch (_slider.direction)
            {
                case Slider.Direction.LeftToRight:
                    if (arrow == Arrow.Left) _targetVal -= _delta;
                    else if (arrow == Arrow.Right) _targetVal += _delta;
                    break;
                case Slider.Direction.RightToLeft:
                    if (arrow == Arrow.Left) _targetVal += _delta;
                    else if (arrow == Arrow.Right) _targetVal -= _delta;
                    break;
                case Slider.Direction.BottomToTop:
                    if (arrow == Arrow.Up) _targetVal += _delta;
                    else if (arrow == Arrow.Down) _targetVal -= _delta;
                    break;
                case Slider.Direction.TopToBottom:
                    if (arrow == Arrow.Up) _targetVal -= _delta;
                    else if (arrow == Arrow.Down) _targetVal += _delta;
                    break;
            }

            _targetVal = Mathf.Clamp01(_targetVal);
        }

        private void Update()
        {
            if (!_isDraging && this._slider)
            {
                this._slider.value = Mathf.Lerp(this._slider.value, this._targetVal, Time.deltaTime * 10);
            }
        }

        [SerializeField]
        private bool _isDraging;
        void IBeginDragHandler.OnBeginDrag(PointerEventData eventData)
        {
            this._isDraging = true;
        }
        void IEndDragHandler.OnEndDrag(PointerEventData eventData)
        {
            this._isDraging = false;
            this._targetVal = _slider.value;
            this._targetVal = Mathf.Clamp01(this._targetVal);

        }

        protected override Color GetFKColor() => Color.blue;
    }
}
